/**    
 * 文件名：CommonUtils.java</br>
 *    
 * 版本信息：v1.0</br>
 * 日期：2017年7月12日</br>
 * © 2005-2017 雷技信息科技（上海）有限公司版权所有</br>
 *
 */
package com.logic.landseaserver.common.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.URL;
import java.util.Enumeration;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.logic.landseaserver.common.LandseaErrorMessageEnum;
import com.logic.landseaserver.common.exception.LandseaException;
import com.logic.landseaserver.common.exception.ResultCode;
import com.logic.landseaserver.common.exception.ResultCodes.CommonResultCode;

/**
 *
 * 项目名称：system-server</br>
 * 类名称：CommonUtils</br>
 * 类描述：常用的零散公共工具</br>
 * 创建人：Aaron</br>
 * 创建时间：2017年7月12日 下午5:29:16</br>
 * 
 * @version 1.0
 *
 */
public abstract class CommonUtils
{
    private static final Logger LOGGER = LoggerFactory.getLogger(CommonUtils.class);
    
    /**
     * 
     * [简要描述]：校验对象参数是否为空</br>
     * [详细描述]：为空抛出异常</br>
     * [作者]：Aaron(2017-06-27)</br>
     *
     * @param input 待检查对象
     * @param errorEnum 异常码
     * @throws LandseaException 异常
     *
     */
    public static void checkEmpty(Object input, LandseaErrorMessageEnum errorEnum)
        throws LandseaException
    {
        if (null == input)
        {
            throw LandseaException.createException(errorEnum);
        }
        
        if (input instanceof String && StringUtils.EMPTY.equals(input))
        {
            throw LandseaException.createException(errorEnum);
        }
    }
    
    /**
     * 
     * [简要描述]：校验对象参数是否为空</br>
     * [详细描述]：为空抛出异常</br>
     * [作者]：Aaron(2017-07-12)</br>
     *
     * @param input 待检查对象
     * @param resultCode 为空时抛出的异常码
     * @throws LandseaException 异常
     *
     */
    public static void checkEmpty(Object input, ResultCode resultCode)
        throws LandseaException
    {
        if (null == input)
        {
            throw LandseaException.createException(resultCode);
        }
        
        if (input instanceof String && StringUtils.EMPTY.equals(input))
        {
            throw LandseaException.createException(resultCode);
        }
    }
    
    /**
     * 
     * [简要描述]：根据资源路径获取文件</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-07-22)</br>
     *
     * @param resourcePath
     * @return
     * @throws LandseaException
     *
     */
    public static URL getResourceUrl(String resourcePath)
        throws LandseaException
    {
        try
        {
            URL url = SendHttpMsgHandler.class.getResource(resourcePath);
            return url;
        }
        catch (Exception e)
        {
            LOGGER.error("SendHttpMsgHandler|getResourceUrl|get resource url failed. resourcePath=" + resourcePath, e);
            throw LandseaException.createException(CommonResultCode.E00050002, e);
        }
    }
    
    /**
     * 
     * [简要描述]：获取本机局域网IP</br>
     * [详细描述]：</br>
     * [作者]：Aaron(2017-08-01)</br>
     *
     * @return
     *
     */
    public static String getLocalLanIpAddress()
    {
        String ip = null;
        try
        {
            for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces
                .hasMoreElements();)
            {
                NetworkInterface networkInterface = interfaces.nextElement();
                if (networkInterface.isLoopback() || networkInterface.isVirtual() || !networkInterface.isUp())
                {
                    continue;
                }
                Enumeration<InetAddress> addresses = networkInterface.getInetAddresses();
                if (addresses.hasMoreElements())
                {
                    ip = addresses.nextElement().getHostAddress();
                }
            }
        }
        catch (Exception e)
        {
            LOGGER.error("Error when getting local lan ip address.", e);
        }
        return ip;
    }
}
